package com.jic.member.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.Feedback;
import com.jic.member.entity.MememberUser;
import com.jic.member.mapper.FeedbackMapper;
import com.jic.member.request.AddFeedBackRequest;
import com.jic.member.service.FeedbackService;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 意见反馈信息表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-03 10:24:09
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

	@Resource
	private FeedbackMapper feedbackMapper;

	@Override
	public long insert(Feedback feedback) {
		//修改
		final Long flag;
		if (null != feedback.getId() && !"".equals(feedback.getId())) {
			feedback.setUpdater(feedback.getAdminOperate().getOperator());
			feedback.setUserId(feedback.getAdminOperate().getOperator());//回复人id
			feedback.setReplytime(new Date());//回复时间
			feedback.setReplyName(feedback.getAdminOperate().getOperateUsername());//回复人
			feedback.setStatus(1L);//回复状态
			flag = feedbackMapper.update(feedback);
		} else {
			flag = feedbackMapper.insert(feedback);
		}
		return flag;
	}

	@Override
	public long delete(Long id) {
		return feedbackMapper.delete(id);
	}

	@Override
	public long update(Feedback feedback) {
		return feedbackMapper.update(feedback);
	}

	@Override
	public Feedback queryByPrimaryKey(Long id) {
		return feedbackMapper.queryByPrimaryKey(id);
	}

	@Override
	public List<Feedback> query(Feedback feedback) {
		return feedbackMapper.query(feedback);
	}

	@Override
	public PageResult<Feedback> queryPage(Feedback feedback, Page pageQuery) {
		//计算下标
		int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
		if (feedback.getAdminOperate().getDeptType().getCode() != 0) {
			feedback.setDeptId(feedback.getAdminOperate().getDeptId());
		}
		feedback.setDeleteFlag(0L);
		List<Feedback> list = feedbackMapper.queryPage(startIndex, pageQuery.getLimit(), feedback);
		long count = feedbackMapper.queryPageCount(feedback);
		PageResult pageResult = new PageResult();
		pageResult.setRows(list);
		pageResult.setTotal(count);
		return pageResult;
	}

	@Override
	public RestResult insertFeedBack(MememberUser member, AddFeedBackRequest addFeedBackRequest) {
		Feedback feedback = new Feedback();
		feedback.setDeleteFlag(0L);
		feedback.setPid(0L);
		feedback.setContent(addFeedBackRequest.getContent());
		feedback.setMemberId(member.getId());
		feedback.setStatus(0L);
		feedback.setCreateTime(new Date());
		feedback.setCreator(0L);
		//        feedback.setDeptId(addFeedBackRequest.getAdminOperate().getDeptId());
		long insert = feedbackMapper.insert(feedback);
		return RestResult.success(insert);
	}

}